package com.qf.factory;

import javax.naming.event.ObjectChangeListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class MyFactory {
    private static final Map<String,Object> beans = new HashMap<>();
    static{
        try {
            InputStream is = MyFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            Properties properties = new Properties();
            properties.load(is);
            System.out.println(properties);
            //遍历属性对象
            Enumeration<?> enumeration = properties.propertyNames();
            while (enumeration.hasMoreElements()) {
                String key = (String) enumeration.nextElement();
                String className = properties.getProperty(key);
                Class<?> clazz = Class.forName(className);
                Object obj = clazz.newInstance();
                //添加对象到容器中
                beans.put(key,obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("属性加载流失败");
        }
    }
    public static Object getBean(String key){
        return beans.get(key);
    }

    public static void main(String[] args) {
        Object userDao = MyFactory.getBean("userDao");
        System.out.println(userDao);
    }
}
